﻿/**  版本信息模板在安装目录下，可自行修改。
* pro_lot.cs
*
* 功 能： N/A
* 类 名： pro_lot
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2017/2/28 13:08:39   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Astronergy.MES.App.Library.MySQL.DAL
{
	/// <summary>
	/// 数据访问类:pro_lot
	/// </summary>
	public partial class pro_lot
	{
		public pro_lot()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("VERSION_NO", "pro_lot"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string LOT_NO,int VERSION_NO,string LOCATION)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from pro_lot");
			strSql.Append(" where LOT_NO=@LOT_NO and VERSION_NO=@VERSION_NO and LOCATION=@LOCATION ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@LOT_NO", MySqlDbType.VarChar,50),
					new MySqlParameter("@VERSION_NO", MySqlDbType.Int32,11),
					new MySqlParameter("@LOCATION", MySqlDbType.VarChar,45)			};
			parameters[0].Value = LOT_NO;
			parameters[1].Value = VERSION_NO;
			parameters[2].Value = LOCATION;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Astronergy.MES.App.Library.MySQL.Model.pro_lot model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into pro_lot(");
			strSql.Append("LOT_NO,VERSION_NO,BARCODE,PART_NUMBER,ORDER_NUMBER,PRODUCT_ID,PRODUCT_MODEL,LOCATION,CREATOR,CREATE_TIME,IS_USED)");
			strSql.Append(" values (");
			strSql.Append("@LOT_NO,@VERSION_NO,@BARCODE,@PART_NUMBER,@ORDER_NUMBER,@PRODUCT_ID,@PRODUCT_MODEL,@LOCATION,@CREATOR,@CREATE_TIME,@IS_USED)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@LOT_NO", MySqlDbType.VarChar,50),
					new MySqlParameter("@VERSION_NO", MySqlDbType.Int32,11),
					new MySqlParameter("@BARCODE", MySqlDbType.VarChar,50),
					new MySqlParameter("@PART_NUMBER", MySqlDbType.VarChar,50),
					new MySqlParameter("@ORDER_NUMBER", MySqlDbType.VarChar,50),
					new MySqlParameter("@PRODUCT_ID", MySqlDbType.VarChar,50),
					new MySqlParameter("@PRODUCT_MODEL", MySqlDbType.VarChar,50),
					new MySqlParameter("@LOCATION", MySqlDbType.VarChar,45),
					new MySqlParameter("@CREATOR", MySqlDbType.VarChar,20),
					new MySqlParameter("@CREATE_TIME", MySqlDbType.Timestamp),
					new MySqlParameter("@IS_USED", MySqlDbType.VarChar,1)};
			parameters[0].Value = model.LOT_NO;
			parameters[1].Value = model.VERSION_NO;
			parameters[2].Value = model.BARCODE;
			parameters[3].Value = model.PART_NUMBER;
			parameters[4].Value = model.ORDER_NUMBER;
			parameters[5].Value = model.PRODUCT_ID;
			parameters[6].Value = model.PRODUCT_MODEL;
			parameters[7].Value = model.LOCATION;
			parameters[8].Value = model.CREATOR;
			parameters[9].Value = model.CREATE_TIME;
			parameters[10].Value = model.IS_USED;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Astronergy.MES.App.Library.MySQL.Model.pro_lot model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update pro_lot set ");
			strSql.Append("BARCODE=@BARCODE,");
			strSql.Append("PART_NUMBER=@PART_NUMBER,");
			strSql.Append("ORDER_NUMBER=@ORDER_NUMBER,");
			strSql.Append("PRODUCT_ID=@PRODUCT_ID,");
			strSql.Append("PRODUCT_MODEL=@PRODUCT_MODEL,");
			strSql.Append("CREATOR=@CREATOR,");
			strSql.Append("IS_USED=@IS_USED");
			strSql.Append(" where LOT_NO=@LOT_NO and VERSION_NO=@VERSION_NO and LOCATION=@LOCATION ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BARCODE", MySqlDbType.VarChar,50),
					new MySqlParameter("@PART_NUMBER", MySqlDbType.VarChar,50),
					new MySqlParameter("@ORDER_NUMBER", MySqlDbType.VarChar,50),
					new MySqlParameter("@PRODUCT_ID", MySqlDbType.VarChar,50),
					new MySqlParameter("@PRODUCT_MODEL", MySqlDbType.VarChar,50),
					new MySqlParameter("@CREATOR", MySqlDbType.VarChar,20),
					new MySqlParameter("@IS_USED", MySqlDbType.VarChar,1),
					new MySqlParameter("@LOT_NO", MySqlDbType.VarChar,50),
					new MySqlParameter("@VERSION_NO", MySqlDbType.Int32,11),
					new MySqlParameter("@LOCATION", MySqlDbType.VarChar,45)};
			parameters[0].Value = model.BARCODE;
			parameters[1].Value = model.PART_NUMBER;
			parameters[2].Value = model.ORDER_NUMBER;
			parameters[3].Value = model.PRODUCT_ID;
			parameters[4].Value = model.PRODUCT_MODEL;
			parameters[5].Value = model.CREATOR;
			parameters[6].Value = model.IS_USED;
			parameters[7].Value = model.LOT_NO;
			parameters[8].Value = model.VERSION_NO;
			parameters[9].Value = model.LOCATION;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string LOT_NO,int VERSION_NO,string LOCATION)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pro_lot ");
			strSql.Append(" where LOT_NO=@LOT_NO and VERSION_NO=@VERSION_NO and LOCATION=@LOCATION ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@LOT_NO", MySqlDbType.VarChar,50),
					new MySqlParameter("@VERSION_NO", MySqlDbType.Int32,11),
					new MySqlParameter("@LOCATION", MySqlDbType.VarChar,45)			};
			parameters[0].Value = LOT_NO;
			parameters[1].Value = VERSION_NO;
			parameters[2].Value = LOCATION;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Astronergy.MES.App.Library.MySQL.Model.pro_lot GetModel(string LOT_NO,int VERSION_NO,string LOCATION)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select LOT_NO,VERSION_NO,BARCODE,PART_NUMBER,ORDER_NUMBER,PRODUCT_ID,PRODUCT_MODEL,LOCATION,CREATOR,CREATE_TIME,IS_USED from pro_lot ");
			strSql.Append(" where LOT_NO=@LOT_NO and VERSION_NO=@VERSION_NO and LOCATION=@LOCATION ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@LOT_NO", MySqlDbType.VarChar,50),
					new MySqlParameter("@VERSION_NO", MySqlDbType.Int32,11),
					new MySqlParameter("@LOCATION", MySqlDbType.VarChar,45)			};
			parameters[0].Value = LOT_NO;
			parameters[1].Value = VERSION_NO;
			parameters[2].Value = LOCATION;

			Astronergy.MES.App.Library.MySQL.Model.pro_lot model=new Astronergy.MES.App.Library.MySQL.Model.pro_lot();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Astronergy.MES.App.Library.MySQL.Model.pro_lot DataRowToModel(DataRow row)
		{
			Astronergy.MES.App.Library.MySQL.Model.pro_lot model=new Astronergy.MES.App.Library.MySQL.Model.pro_lot();
			if (row != null)
			{
				if(row["LOT_NO"]!=null)
				{
					model.LOT_NO=row["LOT_NO"].ToString();
				}
				if(row["VERSION_NO"]!=null && row["VERSION_NO"].ToString()!="")
				{
					model.VERSION_NO=int.Parse(row["VERSION_NO"].ToString());
				}
				if(row["BARCODE"]!=null)
				{
					model.BARCODE=row["BARCODE"].ToString();
				}
				if(row["PART_NUMBER"]!=null)
				{
					model.PART_NUMBER=row["PART_NUMBER"].ToString();
				}
				if(row["ORDER_NUMBER"]!=null)
				{
					model.ORDER_NUMBER=row["ORDER_NUMBER"].ToString();
				}
				if(row["PRODUCT_ID"]!=null)
				{
					model.PRODUCT_ID=row["PRODUCT_ID"].ToString();
				}
				if(row["PRODUCT_MODEL"]!=null)
				{
					model.PRODUCT_MODEL=row["PRODUCT_MODEL"].ToString();
				}
				if(row["LOCATION"]!=null)
				{
					model.LOCATION=row["LOCATION"].ToString();
				}
				if(row["CREATOR"]!=null)
				{
					model.CREATOR=row["CREATOR"].ToString();
				}
				if(row["CREATE_TIME"]!=null && row["CREATE_TIME"].ToString()!="")
				{
					model.CREATE_TIME=DateTime.Parse(row["CREATE_TIME"].ToString());
				}
				if(row["IS_USED"]!=null)
				{
					model.IS_USED=row["IS_USED"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select LOT_NO,VERSION_NO,BARCODE,PART_NUMBER,ORDER_NUMBER,PRODUCT_ID,PRODUCT_MODEL,LOCATION,CREATOR,CREATE_TIME,IS_USED ");
			strSql.Append(" FROM pro_lot ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM pro_lot ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.LOCATION desc");
			}
			strSql.Append(")AS Row, T.*  from pro_lot T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "pro_lot";
			parameters[1].Value = "LOCATION";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

